﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
using DTO;
using AutoMapper;

namespace DataAccess
{
    public static class GoalDAO
    {
        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        static GoalDAO()
        {
            XmlConfigurator.Configure();
        }

        public static List<GoalDTO> GetGoalList()
        {
            try
            {
                List<GoalDTO> lstGoal = new List<GoalDTO>();

                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = from Goal in context.GOAL_LIST
                            select Goal;
                foreach (var item in query)
                {
                    GoalDTO dtoGoal = new GoalDTO();
                    dtoGoal = Mapper.Map<GOAL, GoalDTO>(item);
                    lstGoal.Add(dtoGoal);
                }
                return lstGoal;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return null;
            }
        }

        public static bool AddNewGoal(GoalDTO dtoGoal)
        {
            try
            {
                GOAL obj = new GOAL();
                //Mapping                
                obj = Mapper.Map<GoalDTO, GOAL>(dtoGoal);
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();

                //Kiểm tra Goal Type đã tồn tại chưa, chưa có=>phát sinh lỗi
                obj.GOAL_TYPE = context.GOAL_TYPE_LIST.First(gt => gt.GOAL_TYPE_ID == dtoGoal.GOAL_TYPE.GOAL_TYPE_ID);
                //Kiểm tra Match đã tồn tại chưa, chưa có=>phát sinh lỗi
                obj.MATCH = context.MATCH_LIST.First(m => m.MATCH_ID == dtoGoal.MATCH.MATCH_ID);
                //Kiểm tra Player đã tồn tại chưa, chưa có=>phát sinh lỗi
                obj.PLAYER = context.PLAYER_LIST.First(pl => pl.PLAYER_ID == dtoGoal.PLAYER.PLAYER_ID);
                //Team ghi bàn là team của cầu thủ ghi bàn
                obj.PLAYER.TEAMReference.Load();
                obj.TEAM = obj.PLAYER.TEAM;

                context.AddToGOAL_LIST(obj);
                context.SaveChanges(true);

                dtoGoal.GOAL_ID = obj.GOAL_ID;
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool UpdateGoal(GoalDTO dtoGoal)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                GOAL goal = context.GOAL_LIST.First(t => t.GOAL_ID == dtoGoal.GOAL_ID);
                goal.GOAL_TIME = dtoGoal.GOAL_TIME;
                //Update Goal Type
                goal.GOAL_TYPEReference.Load();
                if (dtoGoal.GOAL_TYPE.GOAL_TYPE_ID != goal.GOAL_TYPE.GOAL_TYPE_ID)
                {
                    goal.GOAL_TYPE = context.GOAL_TYPE_LIST.First(g => g.GOAL_TYPE_ID == dtoGoal.GOAL_TYPE.GOAL_TYPE_ID);
                }
                //Update Match
                goal.MATCHReference.Load();
                if (dtoGoal.MATCH.MATCH_ID != goal.MATCH.MATCH_ID)
                {
                    goal.MATCH = context.MATCH_LIST.First(m => m.MATCH_ID == dtoGoal.MATCH.MATCH_ID);
                }
                //Update Player
                goal.PLAYERReference.Load();
                if (dtoGoal.PLAYER.PLAYER_ID != goal.PLAYER.PLAYER_ID)
                {
                    goal.PLAYER = context.PLAYER_LIST.First(p => p.PLAYER_ID == dtoGoal.PLAYER.PLAYER_ID);
                }
                //Update Team
                goal.TEAMReference.Load();
                if (dtoGoal.TEAM.TEAM_ID != goal.TEAM.TEAM_ID)
                {
                    goal.TEAM = context.TEAM_LIST.First(t => t.TEAM_ID == dtoGoal.TEAM.TEAM_ID);
                }

                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool DeleteAGoal(GoalDTO dtoGoal)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                GOAL obj = context.GOAL_LIST.First(g => g.GOAL_ID == dtoGoal.GOAL_ID);
                context.DeleteObject(obj);
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool DeleteAllGoal()
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = (from goal in context.GOAL_LIST 
                             select goal );
                foreach (var item in query)
                {
                    context.DeleteObject(item);
                }
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }
    }
}
